package cn.javadog.algorithm.e1_100;


/**
 * 整数反转
 * https://leetcode-cn.com/problems/reverse-integer/
 * @author Sprite
 */
public class Algorithm7 {
	public static void main(String[] args) {
		System.out.println(reverse(1534236469));
		System.out.println(reverse(-2147483648));
		System.out.println(reverse(123));
		System.out.println(reverse(-110));
	}


	public static int reverse(int x) {
		boolean fu = x < 0;
		StringBuilder sb = new StringBuilder(fu ? "-" : "0");
		long zheng = fu ? (long)x * -1 : x;
		while (zheng > 0){
			sb.append(zheng % 10);
			zheng = zheng / 10;
		}
		long l = Long.parseLong(sb.toString());
		if (l > Integer.MAX_VALUE || l < Integer.MIN_VALUE){
			return 0;
		}else {
			return (int) l;
		}
	}
}
